package jehc.cloud.gen.web;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jehc.cloud.common.annotation.NeedLoginUnAuth;
import jehc.cloud.common.base.BasePage;
import jehc.cloud.common.base.BaseResult;
import jehc.cloud.common.base.BaseSearch;
import jehc.cloud.common.util.StringUtil;
import jehc.cloud.gen.base.BaseAction;
import jehc.cloud.gen.model.XtDbinfo;
import jehc.cloud.gen.service.XtDbinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc 数据库信息
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/xtDbInfo")
@Api(value = "数据库信息API",description = "数据库信息API")
public class XtDbinfoController extends BaseAction {
	@Autowired
	private XtDbinfoService xtDbinfoService;

	/**
	 * 列表页面
	 * @param model
	 * @return
	 */
	@ApiOperation(value="列表页面", notes="列表页面")
	@RequestMapping(value="/pageList")
	public ModelAndView pageList(Model model) {
		return new ModelAndView("xt-dbinfo/xt-dbinfo-list");
	}

	/**
	 * 新增页面
	 * @param model
	 * @return
	 */
	@ApiOperation(value="新增页面", notes="新增页面")
	@RequestMapping(value="/pageAdd")
	public ModelAndView pageAdd(Model model) {
		return new ModelAndView("xt-dbinfo/xt-dbinfo-add");
	}

	/**
	 * 编辑页面
	 * @param model
	 * @param xt_dbinfo_id
	 * @return
	 */
	@ApiOperation(value="编辑页面", notes="编辑页面")
	@RequestMapping(value="/pageUpdate")
	public ModelAndView pageUpdate(Model model,String xt_dbinfo_id) {
		model.addAttribute("xt_dbinfo_id",xt_dbinfo_id);
		return new ModelAndView("xt-dbinfo/xt-dbinfo-update");
	}

	/**
	 * 详情页面
	 * @param model
	 * @param xt_dbinfo_id
	 * @return
	 */
	@ApiOperation(value="详情页面", notes="详情页面")
	@RequestMapping(value="/pageDetail")
	public ModelAndView pageDetail(Model model,String xt_dbinfo_id) {
		model.addAttribute("xt_dbinfo_id",xt_dbinfo_id);
		return new ModelAndView("xt-dbinfo/xt-dbinfo-detail");
	}

	/**
	* 分页查询
	* @param baseSearch
	*/
	@NeedLoginUnAuth
	@PostMapping(value="/list")
	@ApiOperation(value="分页查询", notes="分页查询")
	@ResponseBody
	public BasePage getXtDbinfoListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonPager(condition,baseSearch);
		List<XtDbinfo> xt_DbinfoList = xtDbinfoService.getXtDbinfoListByCondition(condition);
		PageInfo<XtDbinfo> page = new PageInfo<XtDbinfo>(xt_DbinfoList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	 * 全部集合
	 */
	@NeedLoginUnAuth
	@GetMapping(value="/listAll")
	@ApiOperation(value="全部集合", notes="全部集合")
	@ResponseBody
	public BaseResult listAll(){
		Map<String, Object> condition = new HashMap<>();
		List<XtDbinfo> xtDbinfoList = xtDbinfoService.getXtDbinfoListByCondition(condition);
		return outComboDataStr(xtDbinfoList);
	}

	/**
	* 单条记录
	* @param xt_dbinfo_id 
	* @param request 
	*/
	@NeedLoginUnAuth
	@GetMapping(value="/get/{xt_dbinfo_id}")
	@ApiOperation(value="查询单个数据库信息", notes="查询单个数据库信息")
	@ResponseBody
	public BaseResult getXtDbinfoById(@PathVariable("xt_dbinfo_id")String xt_dbinfo_id, HttpServletRequest request){
		XtDbinfo xt_Dbinfo = xtDbinfoService.getXtDbinfoById(xt_dbinfo_id);
		return outDataStr(xt_Dbinfo);
	}
	/**
	* 添加
	* @param xtDbinfo
	* @param request 
	*/
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个数据库信息", notes="创建单个数据库信息")
	@ResponseBody
	public BaseResult addXtDbinfo(@RequestBody XtDbinfo xtDbinfo,HttpServletRequest request){
		int i = 0;
		if(null != xtDbinfo){
			xtDbinfo.setXt_dbinfo_id(toUUID());
			i=xtDbinfoService.addXtDbinfo(xtDbinfo);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	/**
	* 修改
	* @param xtDbinfo
	* @param request 
	*/
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个数据库信息", notes="编辑单个数据库信息")
	@ResponseBody
	public BaseResult updateXtDbinfo(@RequestBody XtDbinfo xtDbinfo,HttpServletRequest request){
		int i = 0;
		if(null != xtDbinfo){
			i=xtDbinfoService.updateXtDbinfo(xtDbinfo);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	/**
	* 删除
	* @param xt_dbinfo_id 
	* @param request 
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除数据库信息", notes="删除数据库信息")
	@ResponseBody
	public BaseResult delXtDbinfo(String xt_dbinfo_id,HttpServletRequest request){
		int i = 0;
		if(!StringUtil.isEmpty(xt_dbinfo_id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("xt_dbinfo_id",xt_dbinfo_id.split(","));
			i=xtDbinfoService.delXtDbinfo(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
